# -*- coding:utf-8 -*-
#2022/1/18 9:26
# author:xwl
import traceback
import backend.app.app.models.db_owi_private_models as md_pri
from backend.app.app.db.session_private import session_pri
import backend.app.app.models.db_owi_public_models as md_pub
from backend.app.app.db.session_public import session_pub

def get_proj_ID_by_userid(userid):
    try:
        input_projid=list()
        output_projid=list()
        result = session_pri.query(md_pri.TbYearOilstimPlanprojStimevalCalInput.Proj_ID).filter(
            md_pri.TbYearOilstimPlanprojStimevalCalInput.User_ID == userid
        ).all()
        if result:
           for data in result:
                input_projid.append(data[0])
        result = session_pri.query(md_pri.TbYearOilstimPlanprojStimevalCalOutput.Proj_ID).filter(
            md_pri.TbYearOilstimPlanprojStimevalCalOutput.User_ID == userid
        ).all()
        if result:
           for data in result:
                output_projid.append(data[0])
        result=list(set(input_projid)&set(output_projid))
        return result
    except:
        print(traceback.print_exc())
        return False

def save_data_well_caldata(userid, projid, data):
    try:
        db_data = session_pri.query(md_pri.TbUserprojWellCaldata).filter(
            md_pri.TbUserprojWellCaldata.User_ID == userid,
            md_pri.TbUserprojWellCaldata.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'WellCal_Data':data
            })
        else:
            session_pri.add(md_pri.TbUserprojWellCaldata(
                User_ID=userid,
                Proj_ID=projid,
                WellCal_Data=data
            ))
        session_pri.commit()
        return {
            'User_ID':userid,
            'Proj_ID':projid,
            'result':True
        }
    except:
        print(traceback.print_exc())
        session_pri.rollback()
        return False

def save_data_well_data(userid,projid,inputdata,outputdata):
    try:
        save_input_success=dict()
        save_output_success=dict()
        db_data = session_pri.query(
            md_pri.TbUserprojWellInput
        ).filter(
            md_pri.TbUserprojWellInput.User_ID == userid,
            md_pri.TbUserprojWellInput.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'InputData':inputdata
            })
        else:
            session_pri.add(md_pri.TbUserprojWellInput(
                User_ID=userid,
                Proj_ID=projid,
                InputData=inputdata
            ))
        session_pri.commit()
        save_input_success.update({
            'User_ID': userid,
            'Proj_ID': projid,
            'result': True
        })
        db_data = session_pri.query(
            md_pri.TbUserprojWellOut
        ).filter(
            md_pri.TbUserprojWellOut.User_ID == userid,
            md_pri.TbUserprojWellOut.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'OutputData': outputdata
            })
        else:
            session_pri.add(md_pri.TbUserprojWellOut(
                User_ID=userid,
                Proj_ID=projid,
                OutputData=outputdata
            ))
        session_pri.commit()
        save_output_success.update({
            'User_ID': userid,
            'Proj_ID': projid,
            'result': True
        })
        return save_input_success,save_output_success
    except:
        print(traceback.print_exc())
        return False,False

def read_data_well_data(userid,projid):
    try:
        db_data = session_pri.query(md_pri.TbUserprojWellCaldata.WellCal_Data).filter(
            md_pri.TbUserprojWellCaldata.User_ID == userid,
            md_pri.TbUserprojWellCaldata.Proj_ID == projid
        ).first()
        if db_data:
            db_data=db_data[0]
        return db_data
    except:
        print(traceback.print_exc())
        return False

def read_well_input(userid,projid):
    try:
        inputdata=list()
        db_data = session_pri.query(
            md_pri.TbUserprojWellInput.InputData
        ).filter(
            md_pri.TbUserprojWellInput.User_ID == userid,
            md_pri.TbUserprojWellInput.Proj_ID == projid
        ).first()
        if db_data:
            inputdata=db_data[0]
        return inputdata
    except:
        print(traceback.print_exc())
        return False

def read_well_output(userid,projid):
    try:
        outputdata=list()
        db_data = session_pri.query(
            md_pri.TbUserprojWellOut.OutputData
        ).filter(
            md_pri.TbUserprojWellOut.User_ID == userid,
            md_pri.TbUserprojWellOut.Proj_ID == projid
        ).first()
        if db_data:
            outputdata = db_data[0]
        return outputdata
    except:
        print(traceback.print_exc())
        return False

def well_cal_get_projid_by_userid(userid):
    try:
        input_projid=list()
        output_projid=list()
        result = session_pri.query(md_pri.TbOilStimPlanProjWellCalInput.Proj_ID).filter(
            md_pri.TbOilStimPlanProjWellCalInput.User_ID == userid
        ).all()
        if result:
           for data in result:
                input_projid.append(data[0])
        result = session_pri.query(md_pri.TbOilStimPlanProjWellCalOutput.Proj_ID).filter(
            md_pri.TbOilStimPlanProjWellCalOutput.User_ID == userid
        ).all()
        if result:
           for data in result:
                output_projid.append(data[0])
        result=list(set(input_projid)&set(output_projid))
        return result
    except:
        print(traceback.print_exc())
        return False

def well_input_save(User_ID,Proj_ID,Data):
    try:
        db_data = session_pri.query(md_pri.TbOilStimPlanProjWellCalData).filter(
            md_pri.TbOilStimPlanProjWellCalData.User_ID == User_ID,
            md_pri.TbOilStimPlanProjWellCalData.Proj_ID == Proj_ID
        )
        if db_data.first():
            db_data.update({
                'Plan_Well_Data':Data
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjWellCalData(
                User_ID=User_ID,
                Proj_ID=Proj_ID,
                Plan_Well_Data=Data
            ))
        session_pri.commit()
        return {
            'User_ID': User_ID,
            'Proj_ID': Proj_ID,
            'result': True
        }
    except:
        print(traceback.print_exc())
        session_pri.rollback()
        return False

def save_well_input_data(userid,projid,inputdata,outputdata):
    try:
        save_input_success=dict()
        save_output_success=dict()
        db_data = session_pri.query(md_pri.TbOilStimPlanProjWellCalInput).filter(
            md_pri.TbOilStimPlanProjWellCalInput.User_ID == userid,
            md_pri.TbOilStimPlanProjWellCalInput.Proj_ID == projid,
        )
        if db_data.first():
            db_data.update({
                'InputData': inputdata
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjWellCalInput(
                User_ID=userid,
                Proj_ID=projid,
                InputData=inputdata
            ))
        session_pri.commit()
        save_input_success.update({
            'User_ID': userid,
            'Proj_ID': projid,
            'result':True
        })
        db_data = session_pri.query(md_pri.TbOilStimPlanProjWellCalOutput).filter(
            md_pri.TbOilStimPlanProjWellCalOutput.User_ID == userid,
            md_pri.TbOilStimPlanProjWellCalOutput.Proj_ID == projid,
        )
        if db_data.first():
            db_data.update({
                'OutputData':outputdata
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjWellCalOutput(
                User_ID=userid,
                Proj_ID=projid,
                OutputData=outputdata
            ))
        session_pri.commit()
        save_output_success.update({
            'User_ID':userid,
            'Proj_ID':projid,
            'result': True
        })
        return save_input_success,save_output_success
    except:
        print(traceback.print_exc())
        return False,False

def read_well_cal_input(userid,projid):
    try:
        inputdata=list()
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjWellCalInput.InputData
        ).filter(
            md_pri.TbUserprojWellInput.User_ID == userid,
            md_pri.TbUserprojWellInput.Proj_ID == projid
        ).first()
        if db_data:
            inputdata=db_data[0]
        return inputdata
    except:
        print(traceback.print_exc())
        return False

def read_well_cal_output(userid,projid):
    try:
        outputdata=list()
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjWellCalOutput.OutputData
        ).filter(
            md_pri.TbUserprojWellOut.User_ID == userid,
            md_pri.TbUserprojWellOut.Proj_ID == projid
        ).first()
        if db_data:
            outputdata = db_data[0]
        return outputdata
    except:
        print(traceback.print_exc())
        return False

def read_data_well_cal_data(userid,projid):
    try:
        db_data = session_pri.query(md_pri.TbOilStimPlanProjWellCalData.Plan_Well_Data).filter(
            md_pri.TbOilStimPlanProjWellCalData.User_ID == userid,
            md_pri.TbOilStimPlanProjWellCalData.Proj_ID == projid
        ).first()
        if db_data:
            db_data=db_data[0]
        return db_data
    except:
        print(traceback.print_exc())
        return False

def read_stimeval_cal_result_func():
    try:
        db_datas = session_pri.query(
            md_pri.TbYearOilstimPlanprojStimevalCalOutput
        ).all()
        result=list()
        if db_datas:
            result=[data.__dict__ for data in db_datas]
        return result
    except:
        print(traceback.print_exc())
        return False

def unit_scheme_curve_data_recovery_save_data(userid, projid, data):
    try:
        data_db = session_pri.query(md_pri.TbUnitSchemeCurveDataRecovery).filter(
            md_pri.TbUnitSchemeCurveDataRecovery.User_ID == userid,
            md_pri.TbUnitSchemeCurveDataRecovery.Proj_ID == projid
        )
        if data_db.first():
            data_db.update({
                'Data': data
            })
        else:
            session_pri.add(md_pri.TbUnitSchemeCurveDataRecovery(
                User_ID=userid,
                Proj_ID=projid,
                Data=data
            ))
        session_pri.commit()
        return True
    except:
        print(traceback.print_exc())
        session_pri.rollback()
        return False

def unit_scheme_curve_delete_data(delete):
    result = list()
    for data in delete:
        try:
            db_data = session_pub.query(md_pub.TbYearDvpunitOilstimPlanprojEvaluateResult).filter(
                md_pub.TbYearDvpunitOilstimPlanprojEvaluateResult.DvpUnit_OilStim_PlanProj_Name == data[
                    'DvpUnit_OilStim_PlanProj_Name'],
                md_pub.TbYearDvpunitOilstimPlanprojEvaluateResult.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimPlanprojEvaluateResult.Year == data['Year'],
                md_pub.TbYearDvpunitOilstimPlanprojEvaluateResult.StimType_Name == data['StimType_Name'],
            )
            if db_data.first():
                db_data.delete()
                session_pub.commit()
                result.append({
                    'DvpUnit_OilStim_PlanProj_Name': data['DvpUnit_OilStim_PlanProj_Name'],
                    'DvpUnit_Name': data['DvpUnit_Name'],
                    'Year': data['Year'],
                    'StimType_Name': data['StimType_Name'],
                    'result': True,
                    'message': '删除成功'
                })
            else:
                result.append({
                    'DvpUnit_OilStim_PlanProj_Name': data['DvpUnit_OilStim_PlanProj_Name'],
                    'DvpUnit_Name': data['DvpUnit_Name'],
                    'Year': data['Year'],
                    'StimType_Name': data['StimType_Name'],
                    'result': False,
                    'message': '无此数据'
                })
        except Exception as e:
            print(e, e.__traceback__.tb_lineno)
            session_pri.rollback()
            result.append({
                'DvpUnit_OilStim_PlanProj_Name': data['DvpUnit_OilStim_PlanProj_Name'],
                'DvpUnit_Name': data['DvpUnit_Name'],
                'Year': data['Year'],
                'StimType_Name': data['StimType_Name'],
                'result': False,
                'message': '异常'
            })
    return result

def unit_scheme_curve_data_recovery_return_data(userid,projid):
    try:
        data_db = session_pri.query(md_pri.TbUnitSchemeCurveDataRecovery.Data).filter(
            md_pri.TbUnitSchemeCurveDataRecovery.User_ID == userid,
            md_pri.TbUnitSchemeCurveDataRecovery.Proj_ID == projid
        ).first()
        if data_db:
            return data_db[0]
        else:
            return None
    except:
        print(traceback.print_exc())
        return False

def save_data_unit_caldata(userid,projid,data):
    try:
        db_data = session_pri.query(md_pri.TbOilStimPlanProjUnitCalData).filter(
            md_pri.TbOilStimPlanProjUnitCalData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalData.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'Data': data
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjUnitCalData(
                User_ID=userid,
                Proj_ID=projid,
                Data=data
            ))
        session_pri.commit()
        return {
            'User_ID':userid,
            'Proj_ID':projid,
            'result':True
        }
    except:
        print(traceback.print_exc())
        session_pri.rollback()
        return False

def save_data_unit_data(userid,projid,inputdata,outputdata):
    try:
        save_input_success=dict()
        save_output_success=dict()
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjUnitCalInputData
        ).filter(
            md_pri.TbOilStimPlanProjUnitCalInputData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalInputData.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'Input': inputdata
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjUnitCalInputData(
                User_ID=userid,
                Proj_ID=projid,
                Input=inputdata
            ))
        session_pri.commit()
        save_input_success.update({
            'User_ID': userid,
            'Proj_ID': projid,
            'result': True
        })
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjUnitCalOutputData
        ).filter(
            md_pri.TbOilStimPlanProjUnitCalOutputData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalOutputData.Proj_ID == projid
        )
        if db_data.first():
            db_data.update({
                'Output': outputdata
            })
        else:
            session_pri.add(md_pri.TbOilStimPlanProjUnitCalOutputData(
                User_ID=userid,
                Proj_ID=projid,
                Output=outputdata
            ))
        session_pri.commit()
        save_output_success.update({
            'User_ID': userid,
            'Proj_ID': projid,
            'result': True
        })
        return save_input_success,save_output_success
    except:
        print(traceback.print_exc())
        return False,False

def read_unit_input(userid,projid):
    try:
        inputdata=list()
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjUnitCalInputData.Input
        ).filter(
            md_pri.TbOilStimPlanProjUnitCalInputData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalInputData.Proj_ID == projid
        ).first()
        if db_data:
            inputdata=db_data[0]
        return inputdata
    except:
        print(traceback.print_exc())
        return False

def read_unit_output(userid,projid):
    try:
        outputdata=list()
        db_data = session_pri.query(
            md_pri.TbOilStimPlanProjUnitCalOutputData.Output
        ).filter(
            md_pri.TbOilStimPlanProjUnitCalOutputData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalOutputData.Proj_ID == projid
        ).first()
        if db_data:
            outputdata = db_data[0]
        return outputdata
    except:
        print(traceback.print_exc())
        return False

def read_data_unit_data(userid,projid):
    try:
        db_data = session_pri.query(md_pri.TbOilStimPlanProjUnitCalData.Data).filter(
            md_pri.TbOilStimPlanProjUnitCalData.User_ID == userid,
            md_pri.TbOilStimPlanProjUnitCalData.Proj_ID == projid
        ).first()
        if db_data:
            db_data=db_data[0]
        return db_data
    except:
        print(traceback.print_exc())
        return False